#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2022/11/25 9:28
@File: __common__.py
"""
import re

from BladeFerm.Lib.Common import logger


def deal_form_data(input_data_key, input_data_value):
    '''
    对表单的输入数据根据#字段进行检查分类

    :Args:
     - input_data_key: 输入数据的key值
     - input_data_value: 输入数据的value值

    :Returns:
     - key: 去掉key值中的#后部分
     - value: 去掉value值中的#后部分
     - flag: 根据#后部分返回的操作标签，默认返回assign填充
     - label_seq: 根据#后部分返回的元素顺序下标，默认返回1
    '''
    flag = ''
    label_seq = 1
    value_type = 'STRING'
    value_suffix = ''
    input_data_key = re.sub(r'^\d*\|', '', input_data_key, 1)   # key
    keyList = input_data_key.split('#')
    valueList = input_data_value.split('#')
    key = keyList[0]
    if valueList[0].startswith("\""):
        value = [eval(valueList[0])]
    elif valueList[0].startswith("[") and valueList[0].endswith("]"):
        value = valueList[0].strip('[').strip(']').split(',')
        value_type = 'LIST'
    else:
        vl = valueList[0]
        if re.search("，", vl):   # 中文引号替换成英文的
            vl = vl.replace("，", ",")
        value = vl.split(',')
    try:
        if len(keyList) == 2:
            key_suffix = keyList[1]
            # 获取元素序号
            if re.search('\d+', key_suffix):
                label_seq = int(re.search('\d+', key_suffix).group())
        if len(valueList) == 2:
            value_suffix = valueList[1]
        flag = value_suffix
    except Exception as msg:
        logger.error('输入数据格式不正确：' + str(msg))
    finally:
        logger.debug(f"deal_form_data: {key, value, flag, label_seq, value_type}")
    return key, value, flag, label_seq, value_type